package com.wgj.android.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.util.TypedValue;
import android.view.View;

import com.jiaai.xui.XUI;
import com.jiaai.xui.adapter.decoration.DividerDecoration;
import com.jiaai.xui.utils.XDensityUtils;
import com.jiaai.xui.view.widget.XLoadingDialog;
import com.jiaai.xui.view.widget.loadingview.XLoadingView;
import com.wgj.android.R;
import com.wgj.android.adapter.WeiXinGroupAdapter;
import com.wgj.android.entity.WeiXingGroup;
import com.wgj.android.http.WeiXinEvents;
import com.wgj.android.http.weixin.WeiXinCacheUtil;
import com.wgj.android.http.weixin.event.ContactEvent;
import com.wgj.android.zfp.BatchAddService;
import com.wgj.android.zfp.GetWXFriendService;
import com.yanzhenjie.recyclerview.swipe.SwipeMenu;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuBridge;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuCreator;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuItem;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuItemClickListener;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;

/**
 * Created by Administrator on 2017/11/25.
 */

public class AddFriendsActivity extends AppCompatActivity  implements SwipeRefreshLayout.OnRefreshListener{

    @BindView(R.id.groupListView)
    SwipeMenuRecyclerView recyclerView;

    @BindView(R.id.loadingView)
    XLoadingView loadingView;

    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;


    private List<WeiXingGroup> chatGroups=new ArrayList<>();
    private WeiXinGroupAdapter weiXinGroupAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.add_friends_activity);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        loadingView.setOnRetryClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!WeiXinCacheUtil.getInfo().isLogin()){
                    WeiXinLoginActivity.openWeiXingLoginUI(getApplicationContext());
                }else {
                    swipeRefreshLayout.setRefreshing(true);
                    Intent intent=new Intent(getApplication(), GetWXFriendService.class);
                    startService(intent);
                }
            }
        });
        initView();

    }

    public  void  initView(){
        swipeRefreshLayout.setOnRefreshListener(this);
        swipeRefreshLayout.setProgressBackgroundColorSchemeResource(android.R.color.white);
        swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light,
                android.R.color.holo_red_light,android.R.color.holo_orange_light,
                android.R.color.holo_green_light);
        swipeRefreshLayout.setProgressViewOffset(false, 0, (int) TypedValue
                .applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources()
                        .getDisplayMetrics()));
        weiXinGroupAdapter=new WeiXinGroupAdapter(this,chatGroups);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setHasFixedSize(true);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.addItemDecoration(new DividerDecoration(XUI.getColor(R.color.divider), XDensityUtils.dp2px(1)));
        // 创建菜单：
        SwipeMenuCreator mSwipeMenuCreator = new SwipeMenuCreator() {
            @Override
            public void onCreateMenu(SwipeMenu leftMenu, SwipeMenu rightMenu, int viewType) {

                SwipeMenuItem delItem = new SwipeMenuItem(AddFriendsActivity.this);
                delItem.setBackground(R.color.synchro_menu_bg);
                delItem.setText("群加好友");
                delItem.setTextSize(10);
                delItem.setTextColor(Color.WHITE);
                delItem.setWidth(XDensityUtils.dp2px(80));
                delItem.setHeight(MATCH_PARENT);
                rightMenu.addMenuItem(delItem);

            }
        };
        recyclerView.setSwipeMenuCreator(mSwipeMenuCreator);
        recyclerView.setSwipeMenuItemClickListener(new SwipeMenuItemClickListener() {
            @Override
            public void onItemClick(SwipeMenuBridge menuBridge) {

                XLoadingDialog.with(AddFriendsActivity.this).setMessage("正在为你添加好友中,请稍等").show();
                Intent intent=new Intent(getApplication(),BatchAddService.class);
                intent.putExtra("nickName",chatGroups.get(menuBridge.getAdapterPosition()).get_Id());
                startService(intent);
                menuBridge.closeMenu();//关闭菜单
            }
        });
        recyclerView.setAdapter(weiXinGroupAdapter);
        refresh();
    }






    public  void refresh(){
        if(!WeiXinCacheUtil.getInfo().isLogin()){
            loadingView.showError("请先扫码登录微信");
            swipeRefreshLayout.setRefreshing(false);
            return;
        }
        chatGroups.clear();
        List<WeiXingGroup> groups=WeiXinCacheUtil.getWeiXinGroups(WeiXinCacheUtil.getInfo().getWxuin());
        if(groups!=null && groups.size()>0){
            chatGroups.addAll(groups);
            weiXinGroupAdapter.notifyDataSetChanged();
            loadingView.showContent();
        }else {
            loadingView.showEmpty("亲,没有找到你的微信群哦!");
        }
        swipeRefreshLayout.setRefreshing(false);
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetFirendsEvent(ContactEvent event){
        XLoadingDialog.with(this).dismiss();
        refresh();
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public  void onWeiXinLogin(WeiXinEvents events){
        if(events.getType()==WeiXinEvents.EventsType.LOGIN){
            swipeRefreshLayout.setRefreshing(true);
           // XLoadingDialog.with(this).setMessage("正在获取微好友信息,请稍等").show();
            Intent intent=new Intent(getApplication(), GetWXFriendService.class);
            startService(intent);
            return;
        }
        if(events.getType()== WeiXinEvents.EventsType.ADD_FRIEDN_STOP){
            XLoadingDialog.with(AddFriendsActivity.this).dismiss();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onRefresh() {
        if(!WeiXinCacheUtil.getInfo().isLogin()){
            WeiXinLoginActivity.openWeiXingLoginUI(getApplicationContext());
        }else {
            Intent intent=new Intent(getApplication(), GetWXFriendService.class);
            startService(intent);
        }

    }
}
